﻿using System;
using System.Windows.Media.Imaging;
using SomethingSpacial.CustomControls;
using SomethingSpacial.Model;
using SomethingSpacial.Web;
using System.Collections.ObjectModel;
using System.Windows.Ria;

namespace SomethingSpacial.ViewModel
{
    public class LocationsViewModel : ViewModelBase
    {
        private LocationModel MyModel = new LocationModel();
        private SomethingSpacialContext domainContext = new SomethingSpacialContext();
        LoadOperation LoadLocations;

        public LocationsViewModel()
        {
            Locations = new ObservableCollection<LocationModel>();
        }


        public ObservableCollection<LocationModel> Locations
        {
            get;
            private set;
        }

        #region "ViewModel Methods"

        public void loadLocations()
        {
            LoadLocations = domainContext.Load(domainContext.GetLocationsQuery());
            LoadLocations.Completed += new EventHandler(LoadLocations_Completed);
        }

        void LoadLocations_Completed(object sender, EventArgs e)
        {

            //Not sure why this is required.  Wouldn't it have to be "Complete" in order for this Location to be triggered? (ds 09/26/09)
            if (LoadLocations.IsComplete == true)
            {
                foreach (Entity entity in LoadLocations.Entities)
                {
                    Locations Location = entity as Locations;

                    var userGroupLocation = new LocationModel();
                    userGroupLocation.LocationName = Location.LOC_Name;
                    userGroupLocation.LocationID = Location.LOC_Id;
                    userGroupLocation.City = Location.LOC_City;
                    userGroupLocation.Comments = Location.LOC_Comments;
                    userGroupLocation.Country = Location.LOC_Country;
                    userGroupLocation.Lat = Location.LOC_Lat;
                    userGroupLocation.Long = Location.LOC_Long;
                    userGroupLocation.PostalCode = Location.LOC_Postalcode;
                    userGroupLocation.State = Location.LOC_State;
                    userGroupLocation.StreetAddress1 = Location.LOC_StreetAddress1;
                    userGroupLocation.StreetAddress2 = Location.LOC_StreetAddress2;
                    Locations.Add(userGroupLocation);

                }
            }

        }

        public void addLocation(LocationsViewModel LocationNew)
        {
            try
            {
                Locations currLocation = new Locations();

                currLocation.LOC_Id = LocationNew.LocationID;
                currLocation.LOC_Name = LocationNew.LocationName;
                currLocation.LOC_City = LocationNew.City;
                currLocation.LOC_Comments = LocationNew.Comments;
                currLocation.LOC_Country = LocationNew.Country;
                currLocation.LOC_Lat = LocationNew.Lat;
                currLocation.LOC_Long = LocationNew.Long;
                currLocation.LOC_Postalcode = LocationNew.PostalCode;
                currLocation.LOC_State = LocationNew.State;
                currLocation.LOC_StreetAddress1 = LocationNew.StreetAddress1;
                currLocation.LOC_StreetAddress2 = LocationNew.StreetAddress2;

                domainContext.Locations.Add(currLocation);
                domainContext.SubmitChanges();
            }
            catch (Exception exc)
            {
                string message = exc.InnerException.ToString();
            }
        }

        #endregion

        #region "ViewModel Properties"

        public int LocationID
        {
            get { return MyModel.LocationID; }
            set
            {
                if (MyModel.LocationID != value)
                {
                    MyModel.LocationID = value;
                    InvokePropertyChanged("LocationID");
                }
            }
        }


        public string LocationName
        {
            get { return MyModel.LocationName; }
            set
            {
                if (MyModel.LocationName != value)
                {
                    MyModel.LocationName = value;
                    InvokePropertyChanged("LocationName");
                }
            }
        }


        public string StreetAddress1
        {
            get { return MyModel.StreetAddress1; }
            set
            {
                if (MyModel.StreetAddress1 != value)
                {
                    MyModel.StreetAddress1 = value;
                    InvokePropertyChanged("StreetAddress1");
                }
            }
        }

        public string StreetAddress2
        {
            get { return MyModel.StreetAddress2; }
            set
            {
                if (MyModel.StreetAddress2 != value)
                {
                    MyModel.StreetAddress2 = value;
                    InvokePropertyChanged("StreetAddress2");
                }
            }
        }

        public string Comments
        {
            get { return MyModel.Comments; }
            set
            {
                if (MyModel.Comments != value)
                {
                    MyModel.Comments = value;
                    InvokePropertyChanged("Comments");
                }
            }
        }

        public string City
        {
            get { return MyModel.City; }
            set
            {
                if (MyModel.City != value)
                {
                    MyModel.City = value;
                    InvokePropertyChanged("City");
                }
            }
        }

        public string State
        {
            get { return MyModel.State; }
            set
            {
                if (MyModel.State != value)
                {
                    MyModel.State = value;
                    InvokePropertyChanged("State");
                }
            }
        }

        public string PostalCode
        {
            get { return MyModel.PostalCode; }
            set
            {
                if (MyModel.PostalCode != value)
                {
                    MyModel.PostalCode = value;
                    InvokePropertyChanged("PostalCode");
                }
            }
        }


        public string Country
        {
            get { return MyModel.Country; }
            set
            {
                if (MyModel.Country != value)
                {
                    MyModel.Country = value;
                    InvokePropertyChanged("Country");
                }
            }
        }

        public string Lat
        {
            get { return MyModel.Lat; }
            set
            {
                if (MyModel.Lat != value)
                {
                    MyModel.Lat = value;
                    InvokePropertyChanged("Lat");
                }
            }
        }

        public string Long
        {
            get { return MyModel.Long; }
            set
            {
                if (MyModel.Long != value)
                {
                    MyModel.Long = value;
                    InvokePropertyChanged("Long");
                }
            }
        }          
        
        #endregion
    }
}
